package cn.bugstack.domain.strategy.service.rule.chain.factory;

import cn.bugstack.domain.strategy.model.entity.StrategyEntity;
import cn.bugstack.domain.strategy.repository.IStrategyRepository;
import cn.bugstack.domain.strategy.service.rule.chain.ILogicChain;
import cn.bugstack.domain.strategy.service.rule.chain.ILogicChainArmory;
import lombok.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author: Zchy
 * @Description: 抽奖责任链工厂
 * @DateTime: 10/29/2024 5:17 PM
 **/
@Service
public class DefaultLogicChainFactory {
    //原型模式获取对象
    private final ApplicationContext applicationContext;
    //存放策略链 strategyId -> 责任链
    private final Map<Long, ILogicChain> strategyChainGroup;
    private final IStrategyRepository repository;

    @Autowired
    public DefaultLogicChainFactory(ApplicationContext applicationContext, IStrategyRepository repository) {
        this.applicationContext = applicationContext;
        this.strategyChainGroup = new ConcurrentHashMap<>();
        this.repository = repository;
    }

    /*
     * 通过策略id构建责任链
     *
     * @author: Zchy
     * @date: 11/26/2024 6:19 PM
     * @param: strategyId
     * @return: cn.bugstack.domain.strategy.service.rule.chain.ILogicChain
    **/
    public ILogicChain openLogicChain(Long strategyId) {
        ILogicChain cacheLogicChain = strategyChainGroup.get(strategyId);
        if (null != cacheLogicChain) {
            return cacheLogicChain;
        }

        StrategyEntity strategy = repository.queryStrategyEntityByStrategyId(strategyId);
        String[] ruleModels = strategy.getRuleModels();

        //没有配置责任链规则，只装填一个默认责任链
        if(null == ruleModels || ruleModels.length == 0){
            ILogicChain ruleDefaultLogicChain = applicationContext.getBean(LogicModel.RULE_DEFAULT.getCode(), ILogicChain.class);
            strategyChainGroup.put(strategyId, ruleDefaultLogicChain);
            return ruleDefaultLogicChain;
        }

        ILogicChain logicChain = applicationContext.getBean(ruleModels[0], ILogicChain.class);
        ILogicChain current = logicChain;
        for (int i = 1; i < ruleModels.length; i++) {
            ILogicChain nextChain = applicationContext.getBean(ruleModels[i], ILogicChain.class);
            current = current.appendNext(nextChain);
        }
        //最后装配默认责任链
        current.appendNext(applicationContext.getBean(LogicModel.RULE_DEFAULT.getCode(), ILogicChain.class));
        //写入缓存
        strategyChainGroup.put(strategyId, logicChain);
        return logicChain;
    }

    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    public static class StrategyAwardVo {
        private Integer awardId;
        private String ruleModel;
        /**
         * 抽奖奖品规则
         */
        private String awardRuleValue;
    }

    @Getter
    @AllArgsConstructor
    public enum LogicModel {
        RULE_DEFAULT("rule_default", "默认抽奖"),
        RULE_BLACKLIST("rule_blacklist", "黑名单抽奖"),
        RULE_WEIGHT("rule_weight", "权重规则"),
        ;
        private final String code;
        private final String info;
    }
}
